'use strict';

const { app, globalShortcut, BrowserWindow } = require('electron');
const { logger } = require('ee-core/log');

class ElectronBootstrap {
  constructor() {
    this.registerAppEvent();
  }

  registerAppEvent() {
    // 当 Electron 完成初始化时触发
    app.on('ready', () => {
      this.registerGlobalShortcut();
    });

    // 当所有窗口被关闭时触发
    app.on('window-all-closed', () => {
      globalShortcut.unregisterAll();
      if (process.platform !== 'darwin') {
        app.quit();
      }
    });
  }

  registerGlobalShortcut() {
    // 先注销所有快捷键
    globalShortcut.unregisterAll();

    // 注册新快捷键
    try {
      globalShortcut.register('Alt+X', () => {
        logger.info('Shortcut triggered');
        
        // 获取当前窗口
        const win = BrowserWindow.getFocusedWindow() || BrowserWindow.getAllWindows()[0];
        
        if (win) {
          if(win.isVisible()) {
            logger.info('Hiding window');
            win.hide();
          } else {
            logger.info('Showing window');
            win.show();
            win.focus();
          }
        } else {
          logger.error('No window found');
        }
      });

      if (globalShortcut.isRegistered('Alt+X')) {
        logger.info('Shortcut registered successfully');
      } else {
        logger.error('Failed to register shortcut');
      }
    } catch (error) {
      logger.error('Error registering shortcut:', error);
    }
  }
}

module.exports = ElectronBootstrap; 